package br.com.gkarian.mydesk.conversores;

import java.text.MessageFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.ResourceBundle;

import br.com.caelum.vraptor.Convert;
import br.com.caelum.vraptor.Converter;
import br.com.caelum.vraptor.converter.ConversionError;
import br.com.caelum.vraptor.ioc.ApplicationScoped;

@Convert(Calendar.class)
@ApplicationScoped
public class CalendarConverter implements Converter<Calendar> {

	@Override
	public Calendar convert(String value, Class<? extends Calendar> type,
			ResourceBundle bundle) {
		
		if (value == null || value.equals("")) {
			return null;
		}
		try {
			SimpleDateFormat dataFmt = new SimpleDateFormat("dd/MM/yyyy");
			Calendar cal = Calendar.getInstance();
			cal.setTime(dataFmt.parse(value));
			return cal;
		} catch (ParseException e) {
			throw new ConversionError(MessageFormat.format(bundle.getString("data.invalida"), value));
		}
		
	}

}
